<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>政策维护</title>
    <script src="../../../res/js/jquery-3.1.1.min.js"></script>
    <script type="text/javascript" src="../../../res/js/jqueryform.js"></script>
    <script type="text/javascript" src="../../../res/js/util.js"></script>
    <link type="text/css" href="../../../res/layui/css/layui.css" rel="stylesheet" />
    <link type="text/css" href="../../../res/css/stylepulas.css" rel="stylesheet" />
    <script type="text/javascript" src="/ok-admin/js/permissionSetting.js"></script>
</head>
<body>
<style>
    #s{
        background-color: #fff;
        margin-left: -20px;
        width: 60px;
        height: 25px;
        line-height: 5px;
        border: 1px solid #c8ccd4;
        position: absolute;
        top: 435px;
    }
</style>
    <form  method="post" name="myform" id="myform">
    <div id="informContent">
        <p id="phide" style="font-family: MONOSPACE;  font-size: 18px;}">请填写需要发布的内容</p>
        <p id="phide2"></p>
    </div>
        <input type="hidden" name="informContent" id="informContent2">
    <script type="text/javascript" src="../../../res/wangEditor-3.1.1/release/wangEditor.js"></script>
    <script type="text/javascript">
        var E = window.wangEditor
        var informContent = new E('#informContent')
        informContent.create()
    </script>

        <input type="hidden" id="informId" name="informId" >
        <input type="hidden" id="immName" name="immName">

    <div class="item">
        <span>政策名称：</span>
        <label><input type="text" name="informName" autocomplete="off"  maxlength="10"  id="informName" class="width" placeholder="政策名称" /></label>
        <span>消息备注：</span>
        <label><input type="text" name="remark" maxlength="20" id="remark" autocomplete="off" class="width" placeholder="消息备注" /></label>
    </div>

    <div class="iteme">
        <div style="height: 0px;
    margin-left: -60px;
    width: 40px;"><button type="button"  id="s" >全选</button></div>
                <svg class="inline-svg">
                    <symbol id="check" viewbox="0 0 12 10">
                           <polyline points="1.5 6 4.5 9 10.5 1"></polyline>
                       </symbol>
                 </svg>
    </div>

    <div class="item">
        <span></span>
        <div id="div" style="margin-left: 310px">
            <label><button type="button" id="btn" class="layui-btn layui-btn-lg">提交</button></label>
            <label><button type="button" class="layui-btn layui-btn-lg" id="btt">重置</button></label>
        </div>
    </div>

    </form>

    <script>
        $(function () {

            $.ajaxSetup({
                async: false
            });

            url = "/inform/queryAll"
            if (returnUser()) {
                $.post(url, null, function (obj) {
                    $.each(obj, function (index, item) {
                        $(".iteme").append('<input class="inp-cbx" name="checkbox" id="morning' + index + '" value=' + item.immId + ' type="checkbox" style="display: none;"/>' +
                            '        <label class="cbx" for="morning' + index + '"><span>' +
                            '  <svg width="12px" height="10px">' +
                            '    <use xlink:href="#check"></use>' +
                            '  </svg>\n' +
                            '  </span><span class="span0">' + item.immName + '</span></label>'
                        );
                    })

                    var ss = 0;
                    $("#s").on("click", function () {
                        ss++;
                        var objNameList = document.getElementsByName("checkbox");
                        if (null != objNameList) {
                            for (var i = 0; i < objNameList.length; i++) {
                                if (objNameList[i].checked == true) {

                                    if (ss % 2 == 0) {
                                        objNameList[i].checked = false;
                                    }
                                } else {
                                    if (ss % 2 == 1) {
                                        objNameList[i].checked = true;
                                    }
                                }
                            }
                        }
                    })
                })
            }

            var informId = getQueryString("informId");

            if(informId){
                $.ajax({
                    async: false,
                    type: "post",
                    dataType: "json",
                    url: "/inform/selectById?informId="+informId,
                    data: null,
                    success: function (obj) {
                        $("#informId").val(informId);
                        $("#phide").text("");
                        $("#informName").val(obj.informName);
                        $("#remark").val(obj.remark);
                        $("#phide2").html(obj.informContent);
                        let arr = obj.immName.split(',');
                        $(".iteme input:checkbox[name=checkbox]").each(function (index,item) {
                            $.each(arr,function (index1,item1) {
                                if(item.value==item1){
                                    item.checked = true;
                                }
                            })
                        })
                    }
                })
            }

            $("#informContent").on('click',function () {
                 $("#phide").text("");
            })

            // 重置

            $("#btt").on('click',function(){
                var objNameList= document.getElementsByName("checkbox");
                if(null!=objNameList){
                    for(var i=0;i<objNameList.length;i++){
                        objNameList[i].checked=false;
                    }
                }
                $("#informName").val("");
                informContent.txt.text("");
                $("#remark").val("")
            })

            var a  = 0;

            let zc = 0;

            $("#btn").on("click",function () {

                var informId = getQueryString("informId");

                var objNameList= document.getElementsByName("checkbox");
                for(var i=0;i<objNameList.length;i++){
                    if(objNameList[i].checked==false) {
                        zc += 1;
                    }else{
                        zc=0;
                        break;
                    }
                }

                    var obj=$(".inp-cbx");

                    var s='';

                    for(var i=0; i<obj.length; i++){
                        if(obj[i].checked) s+=obj[i].value+',';  //如果选中，将value添加到变量s中
                    }
                    var ids = s.substring (0,s.length-1);     //去掉最后一个ID后面的逗号

                    $("#immName").val(ids);

                    var value= informContent.txt.html();

                    $("#informContent2").val(value.trim());       // 去掉里边的空格

                    var formData = new FormData(document.getElementById("myform"));

                    if(zc==0){
                        if (informContent.txt.text()=="请填写需要发布的内容"){
                            parent.layer.msg( "你还未填写发布内容");
                        }else if (informContent.txt.text().length>2000){
                            parent.layer.msg( "请删除一些字符 2000字以内");
                        }else if (informContent.txt.text()=="" ){
                            parent.layer.msg("你还未发布任何内容");
                        }else if ($("#informName").val()==""){
                            parent.layer.msg("请为政策设置名称");
                        }else{
                            a++
                            if (a==1){
                                $.ajax({
                                    async: false,
                                    type: "post",
                                    dataType: "json",
                                    url: "/inform/insertAor",
                                    processData: false,
                                    contentType: false,
                                    data: formData,
                                    success: function (obj) {
                                        if (obj.status == 1) {
                                            var index = parent.layer.getFrameIndex(window.name);
                                            parent.table.reload("testReload");
                                            parent.layer.close(index);
                                            parent.layer.msg(obj.message);
                                        } else if (obj.status == 2) {
                                            var index = parent.layer.getFrameIndex(window.name);
                                            parent.table.reload("testReload");
                                            parent.layer.close(index);
                                            parent.layer.msg("请先登陆");
                                        }
                                    }

                                })
                            }
                        }
                    }else{
                        parent.layer.msg( "还未选择需要发布的公司");
                    }

               })
        });

        /**
         *  重置方法
         */

    </script>

</body>
</html>